package com.pic.mgt.dao.syushin;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.RowMapper;

import com.pic.mgt.dao.BasicDao;
import com.pic.mgt.dao.IBusinessListMgtDao;
import com.pic.mgt.utils.IgxCollectionUtils;
import com.pic.mgt.utils.SqlScriptUtils;

// (送多方新增)
public class SyushinBusinessApDao extends BasicDao implements ISyushinBusinessApDao {
	private static final String tableName = "syushin_business_ap";
	
	private final String insertBasicSqlScript;
	private final String deleteByBizNameAndApNameSqlScript;
	private final String findSyusinBusinessApNameByBizNameSqlScript;
	private final String deleteIneffectiveBusinessNameDataSqlScript;
	
	private IBusinessListMgtDao businessListMgtDao;
	
	public SyushinBusinessApDao(IBusinessListMgtDao businessListMgtDao) {
		super();
		
		this.businessListMgtDao = businessListMgtDao;
		
		// 建立 Insert Basic 的 Sql Script
		this.insertBasicSqlScript = createInsertBasicSQLScript();
		
		// 建立 delete by business name 和 ap name 的 Sql Script
		deleteByBizNameAndApNameSqlScript = createDeleteByBusinessNameAndApNameSqlScript();
		
		// 建立 find syushin business ap name by business name 的 Sql Script
		this.findSyusinBusinessApNameByBizNameSqlScript = createFindSyusinBusinessApNameByBizNameSqlScriptSqlScript();
		
		// 建立 delete ineffective business name data 的 Sql Script
		this.deleteIneffectiveBusinessNameDataSqlScript = createDeleteIneffectiveBusinessNameDataSqlScript();
	}
	
	private String createInsertBasicSQLScript() {
		Vector<String> insertFieldsVector =
								new Vector<String>();
		
		insertFieldsVector.add(_businessName);
		insertFieldsVector.add(_apName);
		
		return
			SqlScriptUtils.createNamedParamInsertScript(
										tableName,
										insertFieldsVector);
	}
	
	private String createDeleteByBusinessNameAndApNameSqlScript() {
		return 
			SqlScriptUtils.createNamedParamDeleteScript(
					tableName, _businessName, _apName);
	}
	
	private String createFindSyusinBusinessApNameByBizNameSqlScriptSqlScript() {
		Vector<String> selectFieldsVector = new Vector<String>();

		selectFieldsVector.add(_apName);

		return SqlScriptUtils.createNamedParamQueryScriptWithSingleCondition(
				tableName, selectFieldsVector, _businessName);
	}
	
	private String createDeleteIneffectiveBusinessNameDataSqlScript() {
		return
			SqlScriptUtils.createFinalDeleteScript(tableName,
						_businessName + " not in (SELECT " +
						IBusinessListMgtDao._businessName +
						" FROM " +
						this.businessListMgtDao.getTableName() +
						")");
	}
	
	@Override
	public int deleteByBizNameAndApName(String businessName, String apName)
			throws DataAccessException {
		if (null != businessName && null != apName) {
			Map<String,Object> deleteConditionValuesMap
						= IgxCollectionUtils.convertToMap(_businessName, businessName,
														  _apName, apName);
			return
				super.namedParameterJdbcTemplate.
								update(this.deleteByBizNameAndApNameSqlScript,
										deleteConditionValuesMap);
		}
		else {
			return 0;
		}
	}

	@Override
	public List<String> findSyusinBusinessApNameByBizName(String businessName)
			throws DataAccessException {
		JdbcOperations jdbcOperations = super.namedParameterJdbcTemplate.getJdbcOperations();

		return
			jdbcOperations.query(
					this.findSyusinBusinessApNameByBizNameSqlScript,
					new RowMapper<String>() {
						public String mapRow(ResultSet rs, int rowNum) throws SQLException {
							return rs.getString(_apName);
						}
					  }
					);
	}

	@Override
	public String getTableName() {
		return tableName;
	}
	
	@Override
	public int insertBasic(Map<String, Object> insertValuesMap)
			throws DataAccessException {
		if (null != insertValuesMap && !insertValuesMap.isEmpty()) {
			return
			super.namedParameterJdbcTemplate.update(this.insertBasicSqlScript,
													insertValuesMap);
		}
		else {
			return 0;
		}
	}
	
	@Override
	public int deleteIneffectiveBusinessNameData() throws DataAccessException {
		JdbcOperations jdbcOperations = super.namedParameterJdbcTemplate.getJdbcOperations();
		
		return jdbcOperations.update(this.deleteIneffectiveBusinessNameDataSqlScript);
			
	};
	
	public static String getSyushinBusinessApTalbeName() {
		return tableName;
	}
}
